/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2018-2020.
 * Description: Only for small features that do not have their own c-code file
 *     If the feature has its own code file,
 *     please use register_sysctl to register the sysctl interface in its own code file.
 * Author: nixiaoming
 * Create: 2018-3-4
 */

#include <linux/sysctl.h>
#include <linux/rtos_sysctl.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/printk.h>
#include <linux/kmemleak.h>
#include <linux/module.h>
#ifdef CONFIG_RTOS_SOFTIRQ_RESTART_ADJUST
#include <linux/interrupt.h>
#endif

#ifdef CONFIG_RTOS_DEBUG_DUMP_USER_PROCESS
#include <linux/rtos_dump.h>
#endif

#ifdef CONFIG_RTOS_RTPC_RPC
#include <linux/rtos_rtpc_rpc.h>
#endif

#ifdef CONFIG_RTOS_IDUMP_FORCE
#include <linux/idump.h>
#endif
/*
 * Define the sysctl variable here
 * Only for small features that do not have their own c-code file
 *
 * If the feature has its own code file,
 * please use register_sysctl to register the sysctl interface in its own code file.
 */

#ifdef CONFIG_RTOS_SOFTIRQ_RESTART_ADJUST
static int max_softirq_restart_max = RTOS_DEFAULT_MAX_SOFTIRQ_RESTART;
extern int max_softirq_restart;
#endif

#ifdef CONFIG_RTOS_NET_SUPPORT_DISABLE_ZERO_ADDRESS
/* For compatibility with 4.4 behavior. This feature is enabled by default after the macro control function is enabled */
int net_ipv4_zero_bt = 1;
EXPORT_SYMBOL(net_ipv4_zero_bt);
#endif

#ifdef CONFIG_RTOS_DISABLE_HIGHATOMIC
#include <linux/mm.h>

int sysctl_disable_highatomic = 1;
#endif

#ifdef CONFIG_RTOS_STRICT_DEVMEM_IOMEM_CONTROL_DEBUG
int sysctl_devmem_log;
#endif

#ifdef CONFIG_RTOS_DEBUG_DUMP_CORRUPTED_STACK
unsigned long sysctl_stack_dump_size = 512;
#endif

#ifdef CONFIG_RTOS_DEBUG_WATCHPOINT_SIGNAL
#include <internal/long_hw_breakpoint.h>
#endif

/*
 * Define the sysctl struct here
 * Only for small features that do not have their own c-code file
 *
 * If the feature has its own code file,
 * please use register_sysctl to register the sysctl interface in its own code file.
 */

static struct ctl_table rtos_kernel_sysctls[] = {
#ifdef CONFIG_RTOS_SOFTIRQ_RESTART_ADJUST
	{
		.procname       = "max_softirq_restart",
		.data           = &max_softirq_restart,
		.maxlen         = sizeof(int),
		.mode           = 0640,
		.proc_handler   = proc_dointvec_minmax,
		.extra1         = SYSCTL_ONE,
		.extra2         = &max_softirq_restart_max,
	},
#endif
#ifdef CONFIG_RTOS_DEBUG_DUMP_USER_PROCESS
	{
		.procname       = "dump_user_process_exception_info",
		.data           = &dump_user_process_exception_info,
		.maxlen         = sizeof(unsigned int),
		.mode           = 0640,
		.proc_handler   = proc_dointvec_minmax,
		.extra1         = SYSCTL_ZERO,
		.extra2         = SYSCTL_ONE,
	},
#ifdef CONFIG_RTOS_KBOX
	{
		.procname       = "dump_user_process_log_size",
		.data           = &dump_user_process_log_size,
		.maxlen         = sizeof(unsigned int),
		.mode           = 0640,
		.proc_handler   = proc_dump_user_process_log_size,
		.extra1         = SYSCTL_ZERO,
	},
#endif
#endif
#ifdef CONFIG_RTOS_NET_SUPPORT_DISABLE_ZERO_ADDRESS
	{
		.procname       = "net_ipv4_zero_bt",
		.data           = &net_ipv4_zero_bt,
		.maxlen         = sizeof(unsigned int),
		.mode           = 0640,
		.proc_handler   = proc_dointvec_minmax,
		.extra1         = SYSCTL_ZERO,
		.extra2         = SYSCTL_ONE,
	},
#endif
#ifdef CONFIG_RTOS_DISABLE_HIGHATOMIC
	{
		.procname	= "disable_highatomic",
		.data		= &sysctl_disable_highatomic,
		.maxlen		= sizeof(sysctl_disable_highatomic),
		.mode		= 0640,
		.proc_handler	= proc_dointvec_minmax,
		.extra1		= SYSCTL_ZERO,
		.extra2		= SYSCTL_ONE,
	},
#endif
#ifdef CONFIG_RTOS_STRICT_DEVMEM_IOMEM_CONTROL_DEBUG
	{
		.procname       = "devmem_log",
		.data           = &sysctl_devmem_log,
		.maxlen         = sizeof(int),
		.mode           = 0640,
		.proc_handler   = proc_dointvec_minmax,
		.extra1         = SYSCTL_ZERO,
		.extra2         = SYSCTL_ONE,
	},
#endif
#ifdef CONFIG_RTOS_DEBUG_DUMP_CORRUPTED_STACK
	{
		.procname	= "stack_dump_size",
		.data           = &sysctl_stack_dump_size,
		.maxlen         = sizeof(unsigned long),
		.mode           = 0640,
		.proc_handler   = proc_dointvec_minmax,
		.extra1         = SYSCTL_ZERO,
		.extra2         = SYSCTL_INT_MAX,
	},
#endif
#ifdef CONFIG_RTOS_IDUMP_FORCE
	{
		.procname       = "idump_trigger_enable",
		.data           = &idump_trigger_enable_by_proc,
		.maxlen         = sizeof(unsigned int),
		.mode           = 0640,
		.proc_handler   = proc_dointvec_minmax,
		.extra1         = SYSCTL_ONE,
		.extra2         = SYSCTL_ONE,
	},
#endif
#ifdef CONFIG_RTOS_RTPC_RPC
	{
		.procname		= "rpc_kernel_enable",
		.data			= &sysctl_rpc_kernel_enable,
		.maxlen			= sizeof(unsigned int),
		.mode			= 0600,
		.proc_handler	= proc_dointvec_minmax,
		.extra1			= SYSCTL_ZERO,
		.extra2			= SYSCTL_TWO,
	},
#endif
#ifdef CONFIG_RTOS_DEBUG_WATCHPOINT_SIGNAL
	{
		.procname	= "watchpoint_signal",
		.data           = &sysctl_watchpoint_signal,
		.maxlen         = sizeof(unsigned int),
		.mode           = 0640,
		.proc_handler   = proc_dointvec_minmax,
		.extra1         = SYSCTL_ZERO,
		.extra2         = SYSCTL_ONE,
	},
#endif
	{}
};

rtos_kernel_sysctls_proc_init(rtos, rtos_kernel_sysctls);

